{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "36d5fa7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d77c0b82",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取图像\n",
    "image = cv2.imread('example.jpg')\n",
    "# 获取图像的高度、宽度和通道数\n",
    "height, width, _ = image.shape\n",
    "# 转换为灰度图像，方便后续处理\n",
    "gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4779d68b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用Sobel算子滤波\n",
    "# 定义Sobel算子在x方向的卷积核\n",
    "sobel_x_kernel = np.array([[-1, 0, 1],\n",
    "                           [-2, 0, 2],\n",
    "                           [-1, 0, 1]])\n",
    "# 定义Sobel算子在y方向的卷积核\n",
    "sobel_y_kernel = np.array([[-1, -2, -1],\n",
    "                           [0, 0, 0],\n",
    "                           [1, 2, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d618e147",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 进行Sobel算子在x方向的滤波\n",
    "sobel_x_result = np.zeros_like(gray_image, dtype=np.int32)\n",
    "for y in range(1, height - 1):\n",
    "    for x in range(1, width - 1):\n",
    "        neighborhood = gray_image[y - 1:y + 2, x - 1:x + 2]\n",
    "        result = (sobel_x_kernel * neighborhood).sum()\n",
    "        sobel_x_result[y, x] = result\n",
    "\n",
    "# 进行Sobel算子在y方向的滤波\n",
    "sobel_y_result = np.zeros_like(gray_image, dtype=np.int32)\n",
    "for y in range(1, height - 1):\n",
    "    for x in range(1, width - 1):\n",
    "        neighborhood = gray_image[y - 1:y + 2, x - 1:x + 2]\n",
    "        result = (sobel_y_kernel * neighborhood).sum()\n",
    "        sobel_y_result[y, x] = result\n",
    "\n",
    "# 合并x和y方向的Sobel滤波结果，取绝对值并转换为合适的数据类型\n",
    "sobel_combined = np.sqrt(sobel_x_result ** 2 + sobel_y_result ** 2).astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5e3d95f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 给定卷积核滤波\n",
    "given_kernel = np.array([[1, 0, -1],\n",
    "                         [2, 0, -2],\n",
    "                         [1, 0, -1]])\n",
    "filtered_image = np.zeros_like(gray_image, dtype=np.int32)\n",
    "for y in range(1, height - 1):\n",
    "    for x in range(1, width - 1):\n",
    "        neighborhood = gray_image[y - 1:y + 2, x - 1:x + 2]\n",
    "        result = (given_kernel * neighborhood).sum()\n",
    "        filtered_image[y, x] = result\n",
    "filtered_image = filtered_image.astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9292aeae",
   "metadata": {},
   "outputs": [],
   "source": [
    "#  可视化图像的颜色直方图\n",
    "hist_b = [0] * 256\n",
    "hist_g = [0] * 256\n",
    "hist_r = [0] * 256\n",
    "\n",
    "# 统计每个通道的像素值出现次数\n",
    "for row in image:\n",
    "    for pixel in row:\n",
    "        b, g, r = pixel\n",
    "        hist_b[b] += 1\n",
    "        hist_g[g] += 1\n",
    "        hist_r[r] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4eb77e9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制直方图\n",
    "plt.bar(range(256), hist_b, color='b', alpha=0.5, label='Blue')\n",
    "plt.bar(range(256), hist_g, color='g', alpha=0.5, label='Green')\n",
    "plt.bar(range(256), hist_r, color='r', alpha=0.5, label='Red')\n",
    "plt.xlabel('Pixel Value')\n",
    "plt.ylabel('Frequency')\n",
    "plt.title('Color Histogram')\n",
    "plt.legend()\n",
    "# 保存直方图图像\n",
    "plt.savefig('color_histogram.png')\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "88c8562f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 提取纹理特征\n",
    "# 这里以水平方向距离为1，垂直方向距离为0为例\n",
    "graycomatrix = np.zeros((256, 256), dtype=np.uint64)\n",
    "for y in range(height - 1):\n",
    "    for x in range(width - 1):\n",
    "        i = gray_image[y, x]\n",
    "        j = gray_image[y + 1, x]\n",
    "        graycomatrix[i, j] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a351b8aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 保存纹理特征为npy格式\n",
    "np.save('texture_features.npy', graycomatrix)\n",
    "# 保存Sobel滤波后的图像\n",
    "cv2.imwrite('sobel_filtered_image.jpg', sobel_combined)\n",
    "# 保存给定卷积核滤波后的图像\n",
    "cv2.imwrite('filtered_image.jpg', filtered_image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "DL",
   "language": "python",
   "name": "dl"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
